﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace FieldDevice.UI
{
    public partial class Oscillometer : UserControl
    {
        public Oscillometer()
        {
            InitializeComponent();
        }
        public Device Device;

        Color curA_Color = Color.Green;
        Color curB_Color = Color.Yellow;
        Color curC_Color = Color.Red;
        Color volA_Color = Color.Blue;
        Color volB_Color = Color.Brown;
        Color volC_Color = Color.Orange;
        public List<DataList> curA = new List<DataList>() ;
        public List<DataList> curB = new List<DataList>() ;
        public List<DataList> curC = new List<DataList>();
        public List<DataList> volA = new List<DataList>();
        public List<DataList> volB = new List<DataList>();
        public List<DataList> volC = new List<DataList>();
        float max_Vol_Value = 500;
        float max_Cur_Value = 100;
        string unit_Cur_Value = "A";
        string unit_Vol_Value = "V";

        /*刷新曲线图*/
        /*****************************************************************************************************/
        public void UpdateReads()
        {
            this.curA.Add(new DataList(this.Device.CurrentReads["IA"]));
            this.curB.Add(new DataList(this.Device.CurrentReads["IB"]));
            this.curC.Add(new DataList(this.Device.CurrentReads["IC"]));
            this.volA.Add(new DataList(this.Device.CurrentReads["VA"]));
            this.volB.Add(new DataList(this.Device.CurrentReads["VB"]));
            this.volC.Add(new DataList(this.Device.CurrentReads["VC"]));
            DialogUnits();
            this.graph.Refresh();
        }
        /******************************************************************************************************/

        ///
        /// 通过存放在List列表中的最后一个数判断电压、电流单位，及坐标大小
        ///
        /******************************************************************************************************/
        public void DialogUnits()
        {
            if (curA[curA.Count - 1].datas > 1000 || curB[curB.Count - 1].datas > 1000 || curC[curC.Count - 1].datas > 1000)
            {
                this.label_cur.Text = "KA";                  //电流的单位为KA
                for (int val = 0; val < curA.Count; val++)
                {
                    curA[val].datas /= 1000;
                    if (max_Cur_Value < curA[val].datas)
                    {
                        max_Cur_Value = curA[val].datas;
                    }
                }
                for (int val = 0; val < curB.Count; val++)
                {
                    curB[val].datas /= 1000;
                    if (max_Cur_Value < curB[val].datas)
                    {
                        max_Cur_Value = curB[val].datas;
                    }
                }
                for (int val = 0; val < curC.Count; val++)
                {
                    curC[val].datas /= 1000;
                    if (max_Cur_Value < curC[val].datas)
                    {
                        max_Cur_Value = curC[val].datas;
                    }
                }
            }
            else
            {
                this.label_cur.Text = "A";                           //电流单位为A
                foreach (DataList value in curA)
                {
                    if (max_Cur_Value < value.datas)
                        max_Cur_Value = value.datas;
                }
                foreach (DataList value in curB)
                {
                    if (max_Cur_Value < value.datas)
                        max_Cur_Value = value.datas;
                }
                foreach (DataList value in curC)
                {
                    if (max_Cur_Value < value.datas)
                        max_Cur_Value = value.datas;
                }
            }
           
            max_Cur_Value *= 1.5f;                      //电流的最大坐标为三相的电流中的最大值的1.5倍

            if (volA[volA.Count - 1].datas > 1000 || volB[volB.Count - 1].datas > 1000 || volC[volC.Count - 1].datas > 1000)
            {
                this.label_vol.Text = "KV";                      //三相的电压为KV
                for (int val = 0; val < curA.Count; val++)
                {
                    volA[val].datas /= 1000;
                    if (max_Vol_Value < volA[val].datas)
                    {
                        max_Vol_Value = volA[val].datas;
                    }
                }
                for (int val = 0; val < volB.Count; val++)
                {
                    volB[val].datas /= 1000;
                    if (max_Vol_Value < volB[val].datas)
                    {
                        max_Vol_Value = volB[val].datas;
                    }
                }
                for (int val = 0; val < volC.Count; val++)
                {
                    volC[val].datas /= 1000;
                    if (max_Vol_Value < volC[val].datas)
                    {
                        max_Vol_Value = volC[val].datas;
                    }
                }
            }
            else
            {
                this.label_vol.Text = "V";                           //电压单位为V
                foreach (DataList value in volA)
                {
                    if (max_Vol_Value < value.datas)
                        max_Vol_Value = value.datas;
                }
                foreach (DataList value in volB)
                {
                    if (max_Vol_Value < value.datas)
                        max_Vol_Value = value.datas;
                }
                foreach (DataList value in volC)
                {
                    if (max_Vol_Value < value.datas)
                        max_Vol_Value = value.datas;
                }
            }
            max_Vol_Value *= 1.5f;                       //电压的最大坐标为三相的电压中的最大值的1.5倍

            this.maxVol_label.Text = max_Vol_Value.ToString("F2");
            this.midVol_label.Text = (max_Vol_Value / 2).ToString("F2");
            this.maxCur_label.Text = max_Cur_Value.ToString("F2");
            this.midCur_label.Text = (max_Cur_Value / 2).ToString("F2");

        }
        /******************************************************************************************************/
        private void graph_Paint(object sender, PaintEventArgs e)
        {
            //graph.DrawingValue(volA, max_Vol_Value, volA_Color, e.Graphics);
            //graph.DrawingValue(volB, max_Vol_Value, volB_Color, e.Graphics);
            //graph.DrawingValue(volC, max_Vol_Value, volC_Color, e.Graphics);
            //graph.DrawingValue(curA, max_Cur_Value, curA_Color, e.Graphics);
            //graph.DrawingValue(curB, max_Cur_Value, curB_Color, e.Graphics);
            //graph.DrawingValue(curC, max_Cur_Value, curC_Color, e.Graphics);
        }
    }
}
